package service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import util.Utilities;

import dao.ProductDAO;
import dto.ListProductPageDTO;
import dto.PagingDTO;
import dto.ProductDTO;

public class ProductManageService {
	private ProductDAO dao;
	private String pictureDir;

	public ProductManageService(ProductDAO dao) {
		this.dao = dao;
	}

	public ProductManageService(ProductDAO dao, String pictureDir) {
		super();
		this.dao = dao;
		this.pictureDir = pictureDir;
	}


	public ArrayList<String> searchAllLocation() throws SQLException{
		return dao.searchAllLocation();
	}

	public ArrayList<String> serchAllCategory() throws SQLException {
		return dao.searchAllCategory();
	}

	public ArrayList<ProductDTO> searchByCondition(String[] byLocation, String[] byCategory) throws SQLException{
		ArrayList<ProductDTO> list = dao.searchByCondition(byLocation, byCategory);
		for(ProductDTO pdto : list){
			pdto.setRegisterDate(Utilities.changeDayFormat((pdto.getRegisterDate())));
		}
		return list;
	}

	public ProductDTO searchByProductId(int productId) throws SQLException{
		ProductDTO pdto = dao.searchByProductId(productId);
		pdto.setRegisterDate(Utilities.changeDayFormat((pdto.getRegisterDate())));
		pdto.setContent(Utilities.changeContentForTextArea(pdto.getContent()));
		//조회수 1증가
		pdto.setReadCnt(pdto.getReadCnt()+1);
		dao.setReadCnt(pdto);
		return pdto;
	}

	public void registerProduct(ProductDTO pdto) throws SQLException, IllegalStateException, IOException {
		int productId = dao.getProductNoSeq();//productId 1씩 증가.
		String time = Utilities.getNow();
		pdto.setProductId(productId);
		pdto.setReducedRate(100 - (int)((double)pdto.getReducedPrice()/pdto.getOriginalPrice()*100));
		pdto.setReadCnt(0);
		pdto.setRegisterDate(time);
		pdto.setContent(Utilities.changeContentForDB(pdto.getContent()));
		//업로드 처리
				if(pdto.getProductImage()!=null && !pdto.getProductImage().isEmpty()){
					String fileName = pdto.getProductImage().getOriginalFilename();
					String extention = fileName.substring(fileName.lastIndexOf("."));
					long current = System.currentTimeMillis();
					String newFileName = time + extention;
					pdto.setProductImageFilename(newFileName);
					pdto.getProductImage().transferTo(new File(pictureDir, newFileName));
				}
		//DB에 저장
		dao.registerProduct(pdto);
		pdto.setRegisterDate(Utilities.changeDayTimeFormat(time));
		pdto.setContent(Utilities.changeContentForTextArea(pdto.getContent()));
		
	}

	public void modifyProduct(ProductDTO pdto) throws SQLException, IllegalStateException, IOException{
		String time = Utilities.getNow();
		pdto.setUpdateDate(time);
		ProductDTO tempDTO = dao.searchByProductId(pdto.getProductId());
		pdto.setRegisterDate((tempDTO.getRegisterDate()));//등록일
		pdto.setCompanyId(tempDTO.getCompanyId());
		pdto.setReducedRate(100 - (int)((double)pdto.getReducedPrice()/pdto.getOriginalPrice()*100));
		pdto.setContent(Utilities.changeContentForDB(pdto.getContent()));
		//업로드 처리
		if(pdto.getProductImage()!=null &&!pdto.getProductImage().isEmpty()){
			String fileName = pdto.getProductImage().getOriginalFilename();
			String extention = fileName.substring(fileName.lastIndexOf("."));
			long current = System.currentTimeMillis();
			String newFileName = time + extention;
			pdto.setProductImageFilename(newFileName);
			pdto.getProductImage().transferTo(new File(pictureDir, newFileName));
		}
		dao.modifyProduct(pdto);
		pdto.setUpdateDate(Utilities.changeDayFormat(pdto.getUpdateDate()));
		
	}

	public void deleteProduct(int productId) throws SQLException{
		dao.deleteProduct(productId);
		
	}

	public ArrayList<ProductDTO> searchByReadCnt() throws SQLException{
		return dao.searchByReadCnt();
	}

	public ArrayList<ProductDTO> searchByLately() throws SQLException {
		String time = Utilities.getNow();
		return dao.searchByLately(time.substring(0, 7));
	}

	public ArrayList<ProductDTO> searchByRate() throws SQLException{
		return dao.searchByRate();
	}

	public ListProductPageDTO getProductListByPage(int page, String[] byLocation, String[] byCategory) throws SQLException {
		ListProductPageDTO listProductDTO = null;
		ArrayList<ProductDTO> productList = dao.searchListByPaging(page, byLocation, byCategory);
		for(ProductDTO dto : productList){
			dto.setRegisterDate(Utilities.changeDayFormat(dto.getRegisterDate()));
			dto.setContent(Utilities.changeContentForTextArea(dto.getContent()));
		}
		int total = dao.getProductTotal();
		PagingDTO pageDTO = new PagingDTO(total, page);
		listProductDTO = new ListProductPageDTO(productList, pageDTO);
		return listProductDTO;
	}

	public void deleteFile(ProductDTO pdto) throws SQLException {
		File file = new File(pictureDir, pdto.getProductImageFilename());
		dao.updateFileNull(pdto.getProductId());
		file.delete();
		pdto.setProductImageFilename(null);
	}

}
